package com.qing.mvc.validator;

import com.qing.mvc.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
@Component
public class UserValidator implements Validator {
	@Override
	public boolean supports(Class<?> clazz) {
		return User.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmpty(errors,"name",null,"登录名不能为空");
		User user=(User) target;
		if (user.getName().length()>10) {
			errors.rejectValue("name",null,"用户名不能超过10个字符");
		}
	}
}
